<template>
  <div v-loading="isLoading > 0" class="page-container">
    <page-tools>
      <template #left>
        <!-- <span>总记录数: {{ total }} 条</span> -->
      </template>
      <template #right>
        <el-button type="primary" size="mini" @click="addRole">新增角色</el-button>
      </template>
    </page-tools>

    <el-card style="margin-top: 10px;">
      <el-table :data="roleList" border>
        <el-table-column label="序号" type="index" fixed="left" width="100" />
        <el-table-column label="角色id" prop="id" fixed="left" width="100" sortable />
        <el-table-column label="角色名" prop="role_name" width="250" />
        <el-table-column label="是否上线" width="200" sortable>
          <template #default="{ row }">
            <el-switch
              :value="row.status === 1"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeStatus(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="editRole(row)">编辑</el-button>
            <el-button type="text" size="small" @click="distributeAuth(row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <add-edit ref="AddEditDialog" @refresh="getRoleListAll" />
    <distribute-auth ref="DistributeAuth" />
  </div>
</template>

<script>
import { getRoleListAll, updateOneRoleById } from '@/api/role'
import { formatTime } from '@/filters/index'
import AddEdit from './components/AddEdit.vue'
import DistributeAuth from './components/DistributeAuth.vue'
export default {
  name: 'RoleList',
  components: {
    AddEdit,
    DistributeAuth
  },
  filters: {
    formatTime
  },
  data() {
    return {
      isLoading: 0,
      roleList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getRoleListAll()
  },
  mounted() {},
  methods: {
    addRole() {
      this.$refs.AddEditDialog.open()
    },
    editRole(row) {
      this.$refs.AddEditDialog.open(row)
    },
    distributeAuth(row) {
      this.$refs.DistributeAuth.open(row)
    },
    changeStatus(row) {
      this.$confirm('确定要进行此次操作么？', '温馨提示').then(async() => {
        try {
          this.isLoading++
          await updateOneRoleById({
            id: row.id,
            status: row.status === 1 ? -1 : 1
          })

          await this.getRoleListAll()
        } finally {
          this.isLoading--
        }
      }).catch(() => {
        console.log('取消')
      })
    },
    async getRoleListAll() {
      try {
        this.isLoading++
        this.roleList = await getRoleListAll()
      } finally {
        this.isLoading--
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
